package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.CarBusinessInfoBean;
import com.hd.crdao.bean.MessageEvent;
import com.hd.crdao.bean.UserBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.StringUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.ExpandTextView;
import com.hd.crdao.wrapper.GlideCircleTransform;
import com.hd.crdao.wrapper.GlideImageUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/5/19 0019
 */
public class UserActivity extends BaseSecondActivity {
    private static final int CHANGEADDRESS = 101;

    public String mMessageEventLocation;
    public String headimagespath;
    @BindView(R.id.user_head)
    ExpandHeadView mUserHead;
    @BindView(R.id.userinfo_head)
    ExpandTextView mUserinfoHead;
    @BindView(R.id.nickName)
    TextView mNickName;
    @BindView(R.id.nickName_layout)
    LinearLayout mNickNameLayout;
    @BindView(R.id.phonenumber)
    TextView mPhonenumber;
    @BindView(R.id.phonenumber_layout)
    LinearLayout mPhonenumberLayout;
    @BindView(R.id.user_properties)
    TextView mUserProperties;
    @BindView(R.id.companyname)
    TextView mCompanyname;
    @BindView(R.id.companyname_layout)
    LinearLayout mCompanynameLayout;
    @BindView(R.id.userinfo_address)
    LinearLayout mUserinfoAddress;
    @BindView(R.id.tvAddress)
    TextView mTvAddress;
    public CarBusinessInfoBean carBusinessInfoBean;
    public String allAddress;


    @Override
    protected int setContentView() {
        return R.layout.activity_userinfo;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mUserHead;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        initData();
        initListener();
    }


    protected void initListener() {
        mUserinfoHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(UIUtils.getContext(), ChangeHeadPicActivity.class), 0);
                finish();
            }
        });
        mUserinfoAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UIUtils.getContext(), ChangeAddressActivity.class);
                intent.putExtra("allAddress", StringUtils.ParseAddress(allAddress));
                intent.putExtra("dowhat", "update");
                startActivity(intent);
            }
        });

    }


    /**
     * 进入页面需要加载的数据
     */
    protected void initData() {
        String businessid = SPUtils.getString(Config.businessId, "");
        OkHttpUtils.post().url(Constants.GETCARBUSINESS_URL)
                .addParams("appFlag", "true")
                .addParams("id", businessid).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("与服务器连接失败");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                carBusinessInfoBean = new Gson().fromJson(response, CarBusinessInfoBean.class);
                CarBusinessInfoBean.CarBusinessBean carBusinessBean = carBusinessInfoBean.getCarBusiness();
                setSomeParams(carBusinessBean);
            }
        });


    }

    /**
     * 设置一些参数
     * @param carBusinessBean
     */
    private void setSomeParams(CarBusinessInfoBean.CarBusinessBean carBusinessBean) {
        UserBean userBean = carBusinessBean.getUser();

        //头像的设定
        String ImageUrl = carBusinessBean.getCbImage();
        Glide.with(UIUtils.getContext()).load(Constants.IMAGEURL + ImageUrl)
                .bitmapTransform(new GlideCircleTransform(UIUtils.getContext()))
                .placeholder(R.drawable.logo_moren_2x).into(mUserinfoHead.getUserHeadPic());

        //设定昵称
        mNickName.setText(carBusinessBean.getCbContacts());
        //设定手机号码
        mPhonenumber.setText(carBusinessBean.getCbContphone());
        //设定身份
        String[] userTypes = getResources().getStringArray(R.array.userType);
        mUserProperties.setText(userTypes[userBean.getUserType()]);
        //公司名称
        if (TextUtils.isEmpty(carBusinessBean.getCbName())) {
            mCompanyname.setText("请填写");
        } else {
            mCompanyname.setText(carBusinessBean.getCbName());
        }
        String provinceCityArea = carBusinessBean.getAllAreaName();
        String detailAddress = carBusinessBean.getCbAddress();
        allAddress = provinceCityArea + detailAddress;
        if (TextUtils.isEmpty(provinceCityArea) || TextUtils.isEmpty(detailAddress)) {
            mTvAddress.setText("请填写");
        } else {
            mTvAddress.setText(provinceCityArea + detailAddress);
        }
    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEvent(MessageEvent messageEvent) {
        mTvAddress.setText(messageEvent.getAddress());
        mMessageEventLocation = messageEvent.getLocation();
        ALog.d(mMessageEventLocation);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 102) {
            headimagespath = data.getStringExtra("headimagespath");
            if (headimagespath != null) {
                 GlideImageUtils.loadImage(headimagespath,R.drawable.logo_moren_2x,true,mUserinfoHead.getUserHeadPic());
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    @OnClick({R.id.nickName_layout, R.id.phonenumber_layout, R.id.companyname_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.nickName_layout:
                setChangeWhat("昵称");
                break;
            case R.id.phonenumber_layout:
                setChangeWhat("手机号码");
                break;
            case R.id.companyname_layout:
                setChangeWhat("公司名称");
                break;

        }
    }

    /**
     * 跳转到修改页面 需要修改什么?
     *
     * @param changeWhat 修改参数
     */
    public void setChangeWhat(String changeWhat) {
        Intent intent = new Intent(UIUtils.getContext(), ChangeInfoActivity.class);
        intent.putExtra("changeWhat", changeWhat);
        startActivity(intent);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }
}
